using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Cmn;
using EntLibEC.Biz;

using EntLibEC.Biz.RMA;


namespace EntLibEC.View.RMA
{
	/// <summary>
	/// Summary description for RegisterSearch.
	/// </summary>
	public partial class RegisterSearch : PageBase
	{
		protected EntLibEC.View.Cmn.StockProduct ucStockProduct ;
		protected EntLibEC.View.Cmn.RMARequestType ucRMARequestType;
		protected EntLibEC.View.Cmn.RMARevertStatus ucRMARevertStatus;
		protected EntLibEC.View.Cmn.YNStatus ucNewProductStatus;
		protected EntLibEC.View.Cmn.RMAOutBoundStatus ucRMAOutBoundStatus;
		protected EntLibEC.View.Cmn.RMAReturnStatus ucRMAReturnStatus;
		protected EntLibEC.View.Cmn.RMAHandler ucRMAHandler;
		protected EntLibEC.View.Cmn.RMARequestStatus ucStatus;
		protected EntLibEC.View.Cmn.RMARefundStatus ucRefundStatus ;
		protected EntLibEC.View.Cmn.YNStatus ucIsWithin7Days ;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			DataGrid1.PageSize = AppConst.PageSize;
			DataGrid1.PagerStyle.PageButtonCount = AppConst.PageButtonCount;
			txtCreateTo.Attributes["onclick"]= "javascript:calendar()";
			txtCreateFrom.Attributes["onclick"]= "javascript:calendar()";
			txtCheckFrom.Attributes["onclick"]= "javascript:calendar()";
			txtCheckTo.Attributes["onclick"]= "javascript:calendar()";
			txtResponseFrom.Attributes["onclick"]= "javascript:calendar()";
			txtResponseTo.Attributes["onclick"]= "javascript:calendar()";
			txtRecvFrom.Attributes["onclick"]= "javascript:calendar()";
			txtRecvTo.Attributes["onclick"]= "javascript:calendar()";
			txtRefundFrom.Attributes["onclick"]= "javascript:calendar()";
			txtRefundTo.Attributes["onclick"]= "javascript:calendar()";
			txtReturnFrom.Attributes["onclick"]= "javascript:calendar()";
			txtReturnTo.Attributes["onclick"]= "javascript:calendar()";
			txtRevertFrom.Attributes["onclick"]= "javascript:calendar()";
			txtRevertTo.Attributes["onclick"]= "javascript:calendar()";
			txtOutBoundFrom.Attributes["onclick"]= "javascript:calendar()";
			txtOutBoundTo.Attributes["onclick"]= "javascript:calendar()";
			
			if(!Page.IsPostBack)
			{
			    ucRMARequestType.BindRequestType(true);
				ucRMARevertStatus.BindRevertStatus(true);
				ucNewProductStatus.BindStatus(true);
				ucRMAOutBoundStatus.BindStatus(true);
				ucRMAReturnStatus.BindReturnStatus(true);
				ucRMAHandler.BindRMAHandler(true);
				ucStatus.BindRequestStatus(true);
				ucRefundStatus.BindStatus(true);
				ucIsWithin7Days.BindStatus(true);
				Set21DaysSearch();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);

		}
		#endregion

		protected void btnSearch_Click(object sender, System.EventArgs e)
		{
			BindGrid();
		}

		private bool checkForm()
		{
			return true;
		}

		private void BindGrid()
		{
			if ( !checkForm())
				return;

			Hashtable ht = new Hashtable(15);

			if(txtRegisterSysNo.Text.Trim() != AppConst.StringNull)
				ht.Add("RegisterSysNo" , txtRegisterSysNo.Text.Trim());
			if(ucStockProduct.ProductSysNo != AppConst.IntNull)
				ht.Add("ProductSysNo" , ucStockProduct.ProductSysNo);
			if(ucRMARequestType.TypeSysNo != AppConst.IntNull)
				ht.Add("RequestType" , ucRMARequestType.TypeSysNo);
			if(txtSo.Text.Trim() != AppConst.StringNull)
				ht.Add("So" , txtSo.Text.Trim());
			if(txtCreateFrom.Text.Trim() != AppConst.StringNull)
				ht.Add("CreateFrom" , txtCreateFrom.Text.Trim());
			if(txtCreateTo.Text.Trim() != AppConst.StringNull)
				ht.Add("CreateTo" , txtCreateTo.Text.Trim());
			if(txtCheckFrom.Text.Trim() != AppConst.StringNull)
				ht.Add("CheckFrom" , txtCheckFrom.Text.Trim());
			if(txtCheckTo.Text.Trim() != AppConst.StringNull)
				ht.Add("CheckTo" , txtCheckTo.Text.Trim());
			if(ucRMARevertStatus.Status != AppConst.IntNull)
				ht.Add("RevertStatus" , ucRMARevertStatus.Status);
			if(ucNewProductStatus.YNSysNo != AppConst.IntNull)
				ht.Add("NewProductStatus" , ucNewProductStatus.YNSysNo);
			if(ucRMAOutBoundStatus.StatusSysNo != AppConst.IntNull)
				ht.Add("OutBoundStatus" , ucRMAOutBoundStatus.StatusSysNo);
			if(ucRMAReturnStatus.Status != AppConst.IntNull)
				ht.Add("ReturnStatus" , ucRMAReturnStatus.Status);
			if(ucRMAHandler.Handler != AppConst.IntNull)
				ht.Add("NextHandler" , ucRMAHandler.Handler);
			if(ucStatus.Status != AppConst.IntNull)
				ht.Add("Status" , ucStatus.Status);
            if(ucIsWithin7Days.YNSysNo != AppConst.IntNull)
				ht.Add("IsWithin7Days" , ucIsWithin7Days.YNSysNo);


			if(txtResponseFrom.Text.Trim() != AppConst.StringNull)
				ht.Add("ResponseFrom" , txtResponseFrom.Text.Trim());
			if(txtResponseTo.Text.Trim() != AppConst.StringNull)
				ht.Add("ResponseTo" , txtResponseTo.Text);
			

			if(txtRecvFrom.Text.Trim() != AppConst.StringNull)
				ht.Add("RecvFrom" , txtRecvFrom.Text.Trim());
			if(txtRecvTo.Text.Trim() != AppConst.StringNull)
				ht.Add("RecvTo" , txtRecvTo.Text);

			if(txtOutBoundFrom.Text.Trim() != AppConst.StringNull)
				ht.Add("OutBoundFrom" , txtOutBoundFrom.Text.Trim());
			if(txtOutBoundTo.Text.Trim() != AppConst.StringNull)
				ht.Add("OutBoundTo" , txtOutBoundTo.Text);

			if(ucRefundStatus.StatusSysNo != AppConst.IntNull)
				ht.Add("RefundStatus" , ucRefundStatus.StatusSysNo);
			if(txtRefundFrom.Text.Trim() != AppConst.StringNull)
				ht.Add("RefundFrom" , txtRefundFrom.Text.Trim());
			if(txtRefundTo.Text.Trim() != AppConst.StringNull)
				ht.Add("RefundTo" , txtRefundTo.Text.Trim());
			if(txtReturnFrom.Text.Trim() != AppConst.StringNull)
				ht.Add("ReturnFrom" , txtReturnFrom.Text.Trim());
			if(txtReturnTo.Text.Trim() != AppConst.StringNull)
				ht.Add("ReturnTo" , txtReturnTo.Text.Trim());
			if(txtRevertFrom.Text.Trim() != AppConst.StringNull)
				ht.Add("RevertFrom" , txtRevertFrom.Text.Trim());
			if(txtRevertTo.Text.Trim() != AppConst.StringNull)
				ht.Add("RevertTo" , txtRevertTo.Text.Trim());
			

			if ( chkUnRecv.Checked)
				ht.Add("UnRecv", "");

			if ( chkUnCheck.Checked )
				ht.Add("UnCheck", "");

			if ( chkUnResponse.Checked)
				ht.Add("UnResponse", "");

			if ( chkUnOutBound.Checked)
				ht.Add("UnOutBound", "");

			if(chkUnRefund.Checked)
				ht.Add("UnRefund" , "");

			if(chkUnReturn.Checked)
				ht.Add("UnReturn" , "");

			if(chkUnRevert.Checked)
				ht.Add("UnRevert" , "");

			DataSet ds = ConvertDs(RMARegisterManager.GetInstance().GetRegisterDs(ht));

			base.SetDataGridPage(ds, DataGrid1);

			DataGrid1.DataSource = ds;
			DataGrid1.DataBind();
			
			int rows = 0;
			if ( Util.HasMoreRow(ds) )
				rows = ds.Tables[0].Rows.Count;
			Assert(lblMessage, rows.ToString() + " record(s)", 1);
		}

		private DataSet ConvertDs(DataSet ds)
		{
			if ( !Util.HasMoreRow(ds))
				return ds;

			ds.Tables[0].Columns.Add("OutBoundStatusName");
			ds.Tables[0].Columns.Add("RevertStatusName");
			ds.Tables[0].Columns.Add("RefundStatusName");
			ds.Tables[0].Columns.Add("ReturnStatusName");
			ds.Tables[0].Columns.Add("RequestStatusName");
			ds.Tables[0].Columns.Add("NewProduct");
			ds.Tables[0].Columns.Add("Within7Days");

			ds.Tables[0].Columns.Add("RegisterLink");

			foreach( DataRow dr in ds.Tables[0].Rows )
			{
				int outBoundStatus = Util.TrimIntNull(dr["OutBoundStatus"]);
				if ( outBoundStatus != AppConst.IntNull)
					dr["OutBoundStatusName"] = AppEnum.GetRMAOutBoundStatus(outBoundStatus);

				int revertStatus = Util.TrimIntNull(dr["RevertStatus"]);
				if ( revertStatus != AppConst.IntNull )
					dr["RevertStatusName"] = AppEnum.GetRMARevertStatus(revertStatus);

				int refundStatus = Util.TrimIntNull(dr["RefundStatus"]);
				if ( refundStatus != AppConst.IntNull )
					dr["RefundStatusName"] = AppEnum.GetRMARefundStatus(refundStatus);

				int returnStatus = Util.TrimIntNull(dr["ReturnStatus"]);
				if ( returnStatus != AppConst.IntNull )
					dr["ReturnStatusName"] = AppEnum.GetRMAReturnStatus(returnStatus);

				int requestStatus = Util.TrimIntNull(dr["requestStatus"]);
				if ( requestStatus != AppConst.IntNull )
					dr["RequestStatusName"] = AppEnum.GetRMARequestStatus(requestStatus);

				int newProductStatus = Util.TrimIntNull(dr["newproductstatus"]);
				if ( newProductStatus != AppConst.IntNull)
					dr["NewProduct"] = AppEnum.GetYNStatus(newProductStatus);

				dr["RegisterLink"] = "<a href=\"javascript:openWindowS2('RegisterOpt.aspx?sysno=" + dr["registersysno"].ToString() + "')\">" + dr["registersysno"].ToString() + "</a>";

				dr["Within7Days"] = AppEnum.GetYNStatus(dr["IsWithin7Days"]);

			}
			return ds;
		}

		private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			DataGrid1.CurrentPageIndex = e.NewPageIndex;
			BindGrid();
		}

		protected void btn21DaysHandling_Click(object sender, System.EventArgs e)
		{
		    Set21DaysSearch();
		}						

		private void Set21DaysSearch()
		{
			initForm();
		    txtRecvFrom.Text = Util.TrimDateNull(DateTime.Now.AddDays(-21)).ToString(AppConst.DateFormat);
			txtRecvTo.Text = Util.TrimDateNull(DateTime.Now).ToString(AppConst.DateFormat);
			ucStatus.Status = (int)AppEnum.RMARequestStatus.Handling ;
			BindGrid();
		}

		protected void btn_Click(object sender, System.EventArgs e)
		{
			initForm();
			txtRecvFrom.Text = Util.TrimDateNull(DateTime.Now.AddDays(-9)).ToString(AppConst.DateFormat);
			txtRecvTo.Text = Util.TrimDateNull(DateTime.Now).ToString(AppConst.DateFormat);
			ucStatus.Status = (int)AppEnum.RMARequestStatus.Handling ;
			ucIsWithin7Days.YNSysNo = (int)AppEnum.YNStatus.Yes ;
			BindGrid();
		}

		private void initForm()
		{
		    txtRegisterSysNo.Text = "";
			ucStockProduct.ProductSysNo = AppConst.IntNull ;
			ucRMARequestType.TypeSysNo = AppConst.IntNull ;
			txtSo.Text = "";
			txtCreateFrom.Text = "";
			txtCreateTo.Text = "";
			txtRecvFrom.Text = "";
			txtRecvTo.Text = "";
			chkUnRecv.Checked = false;
			txtCheckFrom.Text = "";
			txtCheckTo.Text = "";
			chkUnCheck.Checked = false;
			txtOutBoundFrom.Text = "";
			txtOutBoundTo.Text = "";
			chkUnOutBound.Checked = false;
			txtResponseFrom.Text = "";
			txtResponseTo.Text = "";
			chkUnResponse.Checked = false;
			txtRefundFrom.Text = "";
			txtRefundTo.Text = "";
			chkUnRefund.Checked = false;
			txtReturnFrom.Text = "";
			txtReturnTo.Text = "";
			chkUnReturn.Checked = false;
			ucRMARevertStatus.Status = AppConst.IntNull ;
			ucNewProductStatus.YNSysNo = AppConst.IntNull ;
			ucRMAOutBoundStatus.StatusSysNo = AppConst.IntNull;
			ucRMAReturnStatus.Status = AppConst.IntNull ;
			ucRMAHandler.Handler = AppConst.IntNull;
			ucRefundStatus.StatusSysNo = AppConst.IntNull;
			ucStatus.Status = AppConst.IntNull;
			ucIsWithin7Days.YNSysNo = AppConst.IntNull ;
		}

		protected void btnBeyond21Days_Click(object sender, System.EventArgs e)
		{
			initForm();
			txtRecvTo.Text = Util.TrimDateNull(DateTime.Now.AddDays(-21)).ToString(AppConst.DateFormat);
			ucStatus.Status = (int)AppEnum.RMARequestStatus.Handling;
			BindGrid();
		}

		protected void btnBeyond9Days_Click(object sender, System.EventArgs e)
		{
			initForm();
			txtRecvTo.Text = Util.TrimDateNull(DateTime.Now.AddDays(-9)).ToString(AppConst.DateFormat);
			ucStatus.Status = (int)AppEnum.RMARequestStatus.Handling;
			ucIsWithin7Days.YNSysNo = (int)AppEnum.YNStatus.Yes;
			BindGrid();
		}

		protected void btnClear_Click(object sender, System.EventArgs e)
		{
		    initForm();
		}
		
	}
}
